﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Yelper.Web.Validators;

namespace Yelper.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.IgnoreRoute("favicon.ico");

            routes.MapRoute("Mentions", "Mentions",
                new { controller = "Home", action = "Mentions" });
            routes.MapRoute("Search", "Search/{lambda}",
                new { controller = "Search", action = "Index" });
            routes.MapRoute("Register", "Register", 
                new { controller = "Account", action = "Register" });
            routes.MapRoute("ForgotPwd", "ResetPassword",
                new { controller = "Account", action = "ForgotPwd" });
            routes.MapRoute("Logout", "Logout", 
                new { controller = "Account", action = "Logout" });
            routes.MapRoute("Logon", "Logon", 
                new { controller = "Account", action = "LogOn" });
            routes.MapRoute("Settings", "Settings", 
                new { controller = "Account", action = "Profile" });
            routes.MapRoute("SettingsImage", "Settings/Image", 
                new { controller = "Account", action = "ProfileImageCrop" });
            routes.MapRoute("SettingsPassword", "Settings/Password",
                new { controller = "Account", action = "ChangePwd" });
            routes.MapRoute("YelpPost", "Yelp", 
                new { controller = "Ajax", action = "Yelp" });
            routes.MapRoute("YelpUsers", "Users/{filter}", 
                new { controller = "Ajax", action = "Users" });
            routes.MapRoute("Ajax", "Ajax/{action}", 
                new { controller = "Ajax" });
            routes.MapRoute("Confirm", "Confirm/{guid}", 
                new { controller = "Account", action = "Confirm" });
            routes.MapRoute("Errors", "Errors/{action}", 
                new { controller = "Errors" });

            routes.MapRoute("User", "{username}", 
                new { controller = "Home", action = "UserPage" });
            routes.MapRoute("Permalink", "{username}/{yelpid}", 
                new { controller = "Home", action = "Yelp" });

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            // register custom model validators
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAttribute), typeof(EmailValidator));
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(UrlAttribute), typeof(UrlValidator));
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(UsernameAttribute), typeof(UsernameValidator));
        }

    }
}